草庐IT

javascript oop、instanceof 和基类

全部标签

c++ - 我可以在派生类中为基类的成员起别名吗?

假设我有以下类(class):templateclassBase{protected:TtheT;//...};classDerived:protectedBase,protectedBase{protected://...usingtheInt=Base::theT;//HowdoIaccomplishthis??usingtheFloat=Base::theT;//HowdoIaccomplishthis??};在我的派生类中,我想使用在派生类中更有意义的更直观的名称来引用Base::theT。我使用的是GCC4.7,它很好地覆盖了C++11的特性。有没有一种方法可以使用using

c++ - 对相同基类的引用必须在内存中具有单独的偏移量

我发现这个程序的编译器之间存在一些不一致,structA{};structB:publicA{floatm;};structC:publicA{Bb;floatn;};structD:publicA{floatn;Bb;};static_assert(sizeof(A)==1,"");static_assert(sizeof(B)==4,"");static_assert(sizeof(C)==8,"");//mostcompilerssaythisis12static_assert(sizeof(D)==8,"");大多数编译器断言sizeof(C)==8表示sizeof(C)实际上

c++ - 从模板基类继承构造函数而不重复模板参数?

如何在不重复模板参数(并且不使用宏)的情况下从模板基类继承构造函数:例如,这不起作用(使用GCC4.8):templatestructbase{};templatestructderived:base{usingbase::base;};如果我重复基类的模板参数,它确实有效:templatestructbase{};templatestructderived:base{usingbase::base;};问题是“U”可能是非常复杂的东西,这很烦人并且容易重复错误。例如,这是我最初的激励示例之一:#include#include#include#includeusingnamespace

c++ - 在 C++11 中根据派生类 `operator==` 定义基类 `operator==`?

假设我有一个类型层次结构:structB{...};structD1:B{...};structD2:B{...};...structDn:B{...};每个Di都有自己的operator==定义:structDi:B{booloperator==(constDi&)const{...}...};我现在要定义Boperator==这样:structB{booloperator==(constB&that)const{//psuedo-codeleti,suchthedynamictypeofthisisDiletj,suchthedynamictypeofthatisDjif(i!=j

c++ - 基类和派生类 C++

几天前,我想深入了解C++世界。我正在研究基类和派生类的概念。有人可以解释以下两个代码片段的细微差别吗?classA{private:virtualintGetValue(){return10;}public:intCalculate(){returnGetValue()*1.5;}};classB:publicA{private:virtualintGetValue(){return20;}};intmain(){Bb;std::coutTheoutputis30but15wasexpectedclassA{private:intm_data;public:A():m_data(Ge

c++ - 类型特征以识别主要基类

如果我有一个Base类,它至少有一个虚函数,还有一个Derived类单独继承自它,那么(uintptr_t)derived-(uintptr_t)static_cast(derived)保证(由ItaniumABI)为零,即使Derived不是标准布局。然而,在一般情况下,这不一定是正确的(例如多重继承)。是否可以编写一个特征来检测一个类是否是另一个类的主要基类?ItaniumABI的有用部分:http://refspecs.linux-foundation.org/cxxabi-1.83.html主要基类Foradynamicclass,theuniquebaseclass(ifan

c++ - 在派生类中使用基类 Copy CTOR

我知道有很多关于这个主题的帖子,但我找不到任何帖子来完全回答我的问题。假设我有一个基类和一个派生类,我为它实现了一个CCtor和一个赋值运算符,如下所示:classBase{char*name;....Base(constBase&other):name(nullptr){*this=other}voidoperator=(constBase&other){...Deepcopyofname}}classDerived:publicBase{....Derived(constDerived&other){*this=other;}voidoperator=(constDerived&o

c++ - 调用基类构造函数的 "Using"关键字

我有以下基类classGrammateas{public:Grammateas(std::stringname):_name(name){};virtual~Grammateas(){};private:std::string_name;};和下面的派生类classBoithosfinal:publicGrammateas{public://usingGrammateas::Grammateas;Boithos(inthours):Grammateas("das"),_hours(hours){};virtual~Boithos(){};private:int_hours;};我想使用

c++ - Variadic 模板基类调用转移

在pre-11C++中我有这样的东西:templatestructFoo:T,U,V{boolinit(){if(!T::init()||!U::init()||!V::init())returnfalse;//dolocalinitandreturntrue/false}};我想将其转换为C++11可变参数语法以获得灵活长度参数列表的好处。我理解使用递归解压模板arg列表的概念,但我看不到正确的语法。这是我尝试过的:templatestructFoo:Features...{templateboolrecinit(F&arg,G&&...args){if(!F::init())ret

c++ - 仅为派生自单个基类的类定义模板函数

我有一个基类Base,许多其他类将派生自。我想定义:templateostream&operator但仅适用于从Base派生的类.我需要所有以前定义的operator用于其他类型。怎么做?那可能吗?我无法创建ostream&operator,因为我需要在某些类型特征中使用确切的类型。有什么方法可以在将值作为基类型传递的同时“推送”派生类型吗? 最佳答案 http://www.boost.org/doc/libs/1_46_0/libs/utility/enable_if.htmlhttp://www.boost.org/doc/li